<template>
  <div class="container">
    <el-form :inline="true">
      <el-form-item>
        <el-input v-model="pageGrid.custNo" placeholder="客户编码"></el-input>
      </el-form-item>
      <el-form-item>
        <div class="d-flex">
          <el-button type="primary" @click="getData">搜索</el-button>
          <el-button @click="clearSearch">重置</el-button>
          <!-- <el-upload
            class="upload-demo"
            :action="exportUrl"
            :data="uploadData"
            :limit="1"
            :file-list="fileList"
            :on-success="successUpload"
            :show-file-list="false">
           <el-button size="small" type="primary">导入</el-button> 
          </el-upload>-->
        </div>
      </el-form-item>
    </el-form>
    <el-row style="margin-bottom: 20px">
      <el-button type="primary" @click="addCustom">新增</el-button>
    </el-row>
    <el-table v-loading="isLoading" :data="tableData" border class="table">
      <el-table-column type="index" label="序号"></el-table-column>
      <el-table-column
        prop="custNo"
        label="客户编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="number"
        label="模具编号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="requirements"
        label="特殊要求"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="createTime"
        label="添加时间"
        align="center"
      ></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="editCustom(scope.row)">编辑</el-button>
          <el-button type="text" @click="deleteCustom(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageGrid.pageNo"
        :page-sizes="pageSizeArr"
        :page-size="pageGrid.pageSize"
        layout="total, sizes, prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </div>

    <el-dialog
      title="客户信息"
      :visible.sync="dialogFormVisible"
      width="600px"
      :close-on-click-modal="false"
      @close="closeDialog"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="ruleForm"
      >
        <el-form-item label="客户编码" prop="custNo">
          <el-input
            v-model="ruleForm.custNo"
            :disabled="!!ruleForm.id"
          ></el-input>
        </el-form-item>
        <el-form-item label="模具编号" prop="number">
          <el-input v-model="ruleForm.number"></el-input>
        </el-form-item>
        <el-form-item label="特殊要求" prop="requirements">
          <el-input v-model="ruleForm.requirements" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import api from "../../../api";
import commonInfo from "@/utils/common";
import host from "@/utils/host";

export default {
  name: "custom",
  data() {
    return {
      pageSizeArr: commonInfo.pageSizeArr,
      exportUrl: host.host + "file/open/fileUpload",
      uploadData: {
        type: "custInfo",
      },
      isLoading: false,
      tableData: [],
      total: 0,
      fileList: [],
      pageGrid: {
        pageNo: 1,
        pageSize: 10,
        custNo: null,
      },
      isClick: true,
      dialogFormVisible: false,
      ruleForm: {
        custNo: "",
        number: "",
        requirements: "",
      },
      rules: {
        custNo: [
          { required: true, message: "请输入客户编号", trigger: "blur" },
        ],
        requirements: [
          { required: true, message: "请输入特殊要求", trigger: "blur" },
        ],
        number: [
          { required: true, message: "请输入模具编号", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getData();
  },
  methods: {
    successUpload(response, file, fileList) {
      this.fileList = [];
      api.importStaff({ path: response.data }).then((res) => {
        this.getData();
        this.$message.success("导入成功");
      });
    },
    getData() {
      this.isLoading = true;
      api
        .getCustomInfo(this.pageGrid)
        .then((res) => {
          if (res.code === 200) {
            this.tableData = res.totalElement;
            this.total = res.total;
            this.isLoading = false;
          }
        })
        .catch((this.isLoading = false));
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let send = JSON.parse(JSON.stringify(this.ruleForm));
          if (!this.isClick) return;
          this.isClick = false;
          api.addOrUpdateCustom(send).then(
            (res) => {
              this.delSuccessSave(res);
            },
            (error) => {
              this.isClick = true;
            }
          );
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    delSuccessSave(res) {
      this.isClick = true;
      if (res.code == 200) {
        this.$message.success("保存成功！");
        this.getData();
        this.dialogFormVisible = false;
      }
    },
    closeDialog() {
      this.dialogFormVisible = false;
      this.ruleForm = {
        custNo: "",
        number: "",
        requirements: "",
      };
      this.$refs["ruleForm"].resetFields();
    },
    addCustom() {
      this.ruleForm = {
        custNo: "",
        number: "",
        requirements: "",
      };
      this.dialogFormVisible = true;
    },
    editCustom(row) {
      this.ruleForm = JSON.parse(JSON.stringify(row));
      this.dialogFormVisible = true;
    },
    deleteCustom(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          api.deleteCustom({ custNo: row.custNo }).then((res) => {
            if (res.code == 200) {
              this.$message.success("删除成功！");
              this.pageGrid.pageNo = 1;
              this.getData();
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {});
    },
    clearSearch() {
      this.pageGrid = {
        pageNo: 1,
        pageSize: 10,
        parameter: {
          channel: null,
          state: null,
          mobile: null,
          rbtStatus: null,
        },
      };
      this.getData();
    },
    //分页
    handleCurrentChange(val) {
      this.pageGrid.pageNo = val;
      this.getData();
    },
    //分页
    handleSizeChange(val) {
      this.pageGrid.pageSize = val;
      this.getData();
    },
  },
};
</script>

<style scoped lang="less">
.upload-demo {
  height: 32px;
  display: inline-block;
  margin-left: 10px;
}
.el-upload--text {
  background-color: #fff;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  box-sizing: border-box;
  width: 360px;
  height: 180px;
  text-align: center;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.el-upload--text .el-icon-upload {
  font-size: 67px;
  color: #97a8be;
  margin: 40px 0 16px;
  line-height: 50px;
}

.el-upload--text {
  color: #97a8be;
  font-size: 14px;
  text-align: center;
}

.el-upload--text em {
  font-style: normal;
}
.ruleForm {
  width: 400px;
}
</style>
